﻿// Copyright (c) 2014 Jonathan Magnan (http://jonathanmagnan.com/).
// All rights reserved (http://zzzproject.com/sqlbulkcopy-extensions/).
// Licensed under MIT License (MIT) (https://zsqlbulkcopy.codeplex.com/license/).
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Z.Net.Extensions
{
    internal partial class SqlBulkOperation
    {
        public void GetSqlInformationSqlType()
        {
            string sql = string.Format(SqlGetInformationSqlType, SqlBulkCopy.DestinationTableName);
            Setting.MappingColumnWithDbType = new Dictionary<string, SqlDbType>();

            using (var sqlCommand = new SqlCommand(sql, SqlConnection, SqlTransaction))
            {
                using (IDataReader dr = sqlCommand.ExecuteReader())
                {
                    DataTable schemaTable = dr.GetSchemaTable();

                    if (schemaTable != null)
                    {
                        for (int i = 0; i < dr.FieldCount; i++)
                        {
                            Setting.MappingColumnWithDbType.Add(dr.GetName(i), (SqlDbType) (int) schemaTable.Rows[i]["ProviderType"]);
                        }
                    }
                }
            }
        }
    }
}